import Vue from "vue"
import VueRouter from "vue-router"


Vue.use(VueRouter)

const routes = [


  {
    path: '/',
    component: () => import(/* webpackChunkName: "Home" */ '@/views/HomeView.vue')
    ,
    meta: {
      auth: false,
      Tabbarshow: true,
    },
    children: [
      {
        path: '/',
        name: 'FxhDemoRecommend',
        component: () => import(/* webpackChunkName: "recommend" */"@/views/homes/Recommend.vue"),
        meta: {
          auth: false,
          Tabbarshow: true,
        },
      },
      {
        path: 'promotion',
        name: 'FxhDemoPromotion',
        component: () => import(/* webpackChunkName: "Promotion" */"@/views/homes/Promotion.vue")
      },
      {
        path: 'new',
        name: 'FxhDemoNew',
        component: () => import(/* webpackChunkName: "New" */"@/views/homes/New.vue")
      },
      {
        path: 'hot',
        name: 'FxhDemoHot',
        component: () => import(/* webpackChunkName: "Hot" */"@/views/homes/Hot.vue")
      },

    ]
  },
  {
    path: '/Cate',
    name: 'FxhDemoCateView',
    component: () => import(/* webpackChunkName: "Cate" */ '@/views/CateView.vue'),
    meta: {
      auth: false,
      Tabbarshow: true,
    }
  },
  {
    path: '/Cart',
    name: 'FxhDemoCartView',
    component: () => import(/* webpackChunkName: "Cart" */ '@/views/CartView.vue'),
    meta: {
      auth: false,
      Tabbarshow: true,
    }
  },
  {
    path: '/Mine',
    name: 'FxhDemoMineView',
    component: () => import(/* webpackChunkName: "Mine" */ '@/views/MineView.vue'),
    meta: {
      auth: true,
      Tabbarshow: true
    }
  },
  {
    path: '/Login',
    name: 'FxhDemoLogin',
    component: () => import(/* webpackChunkName: "Login" */ '@/views/Login.vue'),
    meta: {
      auth: false,
      Tabbarshow: false,
    }
  },
  {
    path: '/Details',
    name: 'GoodsDetails',
    component: () => import(/* webpackChunkName: "Details" */ '@/views/homes/GoodsDetails.vue'),
    meta: {
      auth: true,
      Tabbarshow: false,
    }
  },
  {
    path: '/Search',
    name: 'Search',
    component: () => import(/* webpackChunkName: "Search" */ '@/views/homes/Search.vue'),
    meta: {
      auth: false,
      Tabbarshow: false,
    }
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.meta.auth) { //路由元信息里的需登录验证为true
    const token = localStorage.getItem("token") || undefined;
    if (token) next();
    else next(`/Login?redirect=${to.path}`);
  } else {
    next();
  }
});

export default router
